package com.qf.dao.impl;

import com.qf.dao.OrderDetailDao;
import com.qf.domain.OrderDetail;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDetailDaoimpl implements OrderDetailDao {
    QueryRunner qr=new QueryRunner();
    @Override
    public void addOrderDetailimpl(OrderDetail orderDetail) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="insert into tb_orderdetail(id,oid,pid,num,money)values(?,?,?,?,?)";
        Object[] params={orderDetail.getId(),orderDetail.getOid(),orderDetail.getPid(),orderDetail.getNum(),orderDetail.getMoney()};
        try {
            qr.update(connection,sql,params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public OrderDetail selectOrderDetailOne(String oid) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="select * from tb_orderdetail where oid=?";
        try {
           return qr.query(connection,sql,new BeanHandler<>(OrderDetail.class),oid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public List<OrderDetail> selectOrderDetailAll(String oid) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="select * from tb_orderdetail where oid=?";
        try {
            return qr.query(connection,sql,new BeanListHandler<>(OrderDetail.class),oid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }
}
